package com.wutuo.aggragation.classroom.controller;


import cn.apestar.agg.common.security.BaseAuthController;
import cn.apestar.core.base.BaseMessage;
import com.wutuo.classroom.api.LearningClassroomAPIService;
import com.wutuo.classroom.api.request.LearningClassroomRequest;
import com.wutuo.classroom.api.request.ListLearningClassroomRequest;
import com.wutuo.classroom.api.response.LearningClassroomResponse;
import com.wutuo.classroom.provider.domain.LearningClassroom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学堂 <br>
 * 详细说明.
 * <p>
 *
 * @author lvh
 * @version 1.0
 */
@RestController @RequestMapping(value = "/learningClassroom", produces = "application/json")
public class LearningClassroomController extends BaseAuthController {

    private Logger logger = LoggerFactory.getLogger(LearningClassroomController.class);

    @Autowired private LearningClassroomAPIService learningClassroomService;

    /**
     * 新增学堂
     *
     * @param request
     */
    @RequestMapping(value = "/addorUpdate", method = RequestMethod.POST) public Object create(
        @RequestBody @Valid LearningClassroomRequest request) {

        learningClassroomService.addorUpdate(request);
        return new BaseMessage();
    }

    /**
     * 删除学堂
     *
     * @param id
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.GET) public Object del(
        @PathVariable("id") Long id) {

        learningClassroomService.del(id);
        return new BaseMessage();
    }

    /**
     * 查询学堂
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/lisForPage", method = RequestMethod.POST)
    public List<LearningClassroomResponse> lisForPage(
        @RequestBody ListLearningClassroomRequest request) {

        List list = learningClassroomService
            .listLearningClassRoomByUserId(getAppType(), getCurrentUserId(), request);
        return list;
    }

    /**
     * 查询学堂
     *
     * @return
     */
    @RequestMapping(value = "/getLearnClassroomImgById/{id}/resize/{resize}", method = RequestMethod.GET)
    public List<HashMap> getLearnClassroomImgById(@PathVariable("id") Long id,
        @PathVariable("resize") String resize) {

        List list = new ArrayList();
        LearningClassroom learningClassroomResponse = learningClassroomService.find(id);
        String learningImage = learningClassroomResponse.getLearningImage();
        Long pageNum = learningClassroomResponse.getPageNum();
        for (int i = 1; i <= pageNum; i++) {
            String learningImagesUrl = learningImage.replace("{page}", Integer.toString(i));
            learningImagesUrl = learningImagesUrl.replace("{resize}", resize);
            Map map = new HashMap();
            map.put("url", learningImagesUrl);
            map.put("index", i);
            list.add(map);
        }
        return list;
    }

}